#include "Deck.h"
#include "Definitions.h"
#include <random>

Deck::Deck()
{
	this->Reset();
}

Deck::~Deck()
{
	ReleaseThis(m_CardMesh);
	ReleaseThis(m_CardTextureData);
	for (int i=0; i < 13; i++)
		ReleaseThis(m_CardTexture[i]);
}

void Deck::InitCard(LPDIRECT3DDEVICE9& D3DDevice)
{
	D3DXLoadMeshFromX(L"Meshes/Card.x", D3DXMESH_MANAGED, D3DDevice,
			NULL, &m_CardTextureData, NULL, &m_NumTextures, &m_CardMesh);

	D3DXCreateTextureFromFile(D3DDevice, L"Textures/MyBad.png", &m_CardTexture[0]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card1.png", &m_CardTexture[1]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card2.png", &m_CardTexture[2]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card3.png", &m_CardTexture[3]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card4.png", &m_CardTexture[4]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card5.png", &m_CardTexture[5]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card6.png", &m_CardTexture[6]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card7.png", &m_CardTexture[7]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card8.png", &m_CardTexture[8]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card9.png", &m_CardTexture[9]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card10.png", &m_CardTexture[10]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card11.png", &m_CardTexture[11]);
	D3DXCreateTextureFromFile(D3DDevice, L"Textures/Card12.png", &m_CardTexture[12]);
}

void Deck::Reset()
{
	int i = 0;

	// set five 1's
	for (int c=0; c < 5; c++)
	{
		card[i] = 1;
		i++;
	}
	// set four 2's
	for (int c=0; c < 4; c++)
	{
		card[i] = 2;
		i++;
	}
	// set four 3's
	for (int c=0; c < 4; c++)
	{
		card[i] = 3;
		i++;
	}
	// set four 4's
	for (int c=0; c < 4; c++)
	{
		card[i] = 4;
		i++;
	}
	// set four 5's
	for (int c=0; c < 4; c++)
	{
		card[i] = 5;
		i++;
	}
	// set two 6's
	for (int c=0; c < 2; c++)
	{
		card[i] = 6;
		i++;
	}
	// set four 7's
	for (int c=0; c < 4; c++)
	{
		card[i] = 7;
		i++;
	}
	// set four 8's
	for (int c=0; c < 4; c++)
	{
		card[i] = 8;
		i++;
	}
	// set two 9's
	for (int c=0; c < 2; c++)
	{
		card[i] = 9;
		i++;
	}
	// set four 10's
	for (int c=0; c < 4; c++)
	{
		card[i] = 10;
		i++;
	}
	// set four 11's
	for (int c=0; c < 4; c++)
	{
		card[i] = 11;
		i++;
	}
	// set four 12's
	for (int c=0; c < 4; c++)
	{
		card[i] = 12;
		i++;
	}
	// set four Sorry!s
	//for (int c=0; c < 4; c++)
	//{
	//	card[i] = 0;
	//	i++;
	//}

	remaining = 45;
}

int Deck::GetValue()
{
	int value, index;

	//if there are no cards left then it'll draw a blank card
	if (remaining <= 1)
		return 0;
	
	index = (1 + rand() % (remaining -1));
	value = card[index];

	//the card is gone, so remove it move everything up
	for (int i=index; i < remaining - 1; i++)
	{
		card[i] = card[i+1];
	}

	remaining--;

	return value;
}

void Deck::DisCard(int n)
{
	//takes the int and puts it at the end of the deck
	card[remaining] = n;
	remaining++;
}